package com.bootdo.merchant.controller;

import com.bootdo.common.annotation.IpFilter;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.MD5Utils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.merchant.domain.AgentDO;
import com.bootdo.merchant.serivce.AgentService;
import com.bootdo.merchant.serivce.MerchantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

/**
 * @Desc 商户代理管理
 * @auth
 * @date 2019/10/24
 *
 */
@Controller
@RequestMapping("/merchant/agent")
public class AgentController {

    private final String PREFIX = "merchant/agent";

    @Autowired
    private AgentService agentService;
    @Autowired
    private MerchantService merchantService;

    @GetMapping()
    @RequiresPermissions("merchant:agent:agent")
    String agent(Model model) {
        return PREFIX + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("merchant:agent:agent")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<AgentDO> userList = agentService.list(query);
        int total = agentService.count(query);
        PageUtils pageUtils = new PageUtils(userList, total);
        return pageUtils;
    }

    @RequiresPermissions("merchant:agent:add")
    @Log("添加商户代理")
    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("userId", getUserId());
        return PREFIX + "/add";
    }

    @IpFilter
    @RequiresPermissions("merchant:agent:add")
    @Log("保存商户代理用户")
    @PostMapping("/save")
    @ResponseBody
    R save(AgentDO agentDO) {
        agentDO.setPassword(MD5Utils.encrypt(agentDO.getUserName(), agentDO.getPassword()));
        if (agentService.save(agentDO) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:agent:edit")
    @Log("编辑商户代理")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        AgentDO agentDO = agentService.get(id);
        model.addAttribute("user", agentDO);
        return PREFIX + "/edit";
    }

    @IpFilter
    @RequiresPermissions("merchant:agent:edit")
    @Log("更新商户代理")
    @PostMapping("/update")
    @ResponseBody
    R update(AgentDO agentDO) {
        if (agentService.update(agentDO) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:agent:editStatus")
    @Log("停用启用商户代理")
    @PostMapping("/editStatus")
    @ResponseBody
    R editStatus(Long userId, Integer status) {
        if (agentService.editStatus(userId, status) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequiresPermissions("merchant:agent:quota")
    @Log("获取商户流水")
    @GetMapping("/quota/{id}")
    String quota(Model model, @PathVariable("id") Long id) {
        model.addAttribute("id", id);
        return "quota/list";
    }

    @RequiresPermissions("merchant:agent:resetToken")
    @Log("重置商户代理令牌")
    @GetMapping("/reset/{id}")
    String reset(Model model, @PathVariable("id") Long id) {
        model.addAttribute("userId", id);
        return PREFIX + "/reset";
    }

    @RequiresPermissions("merchant:agent:resetToken")
    @Log("重置商户代理令牌")
    @PostMapping("/resetToken")
    @ResponseBody
    @IpFilter
    R resetToken(Long userId) {
        if (agentService.resetToken(userId) > 0) {
            return R.ok();
        }
        return R.error();
    }
}
